﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;

/// <summary>
/// Summary description for DbConnect
/// </summary>
public class DbConnect
{
    public DbConnect()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public string query;
    public string GetResult()
    {
        string rs = string.Empty; 
        using (SqlConnection connection = new SqlConnection())
        {
            connection.ConnectionString = GlobalVar.ConnString;
            connection.Open();
            // Use the connection
            using (SqlCommand command = new SqlCommand())
            {
                command.CommandText = query;
                command.Connection = connection;
                /*using (SqlDataReader reader = command.ExecuteReader())
                {
                    //StringBuilder sb = new StringBuilder();
                    //while (reader.Read())
                    //{
                    //    Label1.Text += "<p><pre><a href=''>" + reader.GetString(0) + "</a>         Last Login: </pre></p>";
                    //}
                }*/
                SqlDataAdapter adapter = new SqlDataAdapter(command.CommandText, connection.ConnectionString);
                DataSet customers = new DataSet();
                adapter.Fill(customers, "user");
                try
                {
                    if (customers.Tables.Count > 0)
                    {
                        for (int index = 0; index < customers.Tables.Count; index++)
                        {
                            if (customers.Tables[index].Rows.Count > 0)
                            {
                                for (int rindex = 0; rindex < customers.Tables[index].Rows.Count; rindex++)
                                {
                                    for (int cindex = 0; cindex < customers.Tables[index].Columns.Count; cindex++)
                                    {
                                        if (customers.Tables[index].Rows[rindex][cindex] == System.DBNull.Value)
                                        {
                                            /*DateTime defdate = new DateTime(2000, 1, 1);
                                            if (customers.Tables[index].Columns[cindex].DataType == System.Type.GetType("System.String"))
                                                customers.Tables[index].Rows[rindex][cindex] = "";
                                            else if (customers.Tables[index].Columns[cindex].DataType == System.Type.GetType("System.Int32"))
                                                customers.Tables[index].Rows[rindex][cindex] = 0;
                                            else if (customers.Tables[index].Columns[cindex].DataType == System.Type.GetType("System.DateTime"))
                                                customers.Tables[index].Rows[rindex][cindex] = defdate;
                                            else if (customers.Tables[index].Columns[cindex].DataType == System.Type.GetType("System.Decimal"))
                                                customers.Tables[index].Rows[rindex][cindex] = 0.0;*/
                                            //System.Guid g = new Guid("00000000000000000000000000000000");
                                            //if (customers.Tables[index].Columns[cindex].DataType == System.Type.GetType("System.Guid"))
                                            //    customers.Tables[index].Rows[rindex][cindex] = g;
                                            //else if (customers.Tables[index].Columns[cindex].DataType == System.Type.GetType("System.Char"))
                                            //    customers.Tables[index].Rows[rindex][cindex] = "1";
                                            //else
                                            //{
                                             //   customers.Tables[index].Columns[cindex].DataType = System.Type.GetType("System.String");
                                                customers.Tables[index].Rows[rindex][cindex] = "0";
                                            //}
                                        }
                                    }
                                }
                            }
                        }
                    }

                }
                catch(Exception ex)
                {
                    int i=0;
                }
                rs += "<count name='row'>"+customers.Tables["user"].Rows.Count+"</count>";
                rs += customers.GetXml();               
            }
            connection.Close();
        }
        return rs;
    }
    public string GetResult2()
    {
        string rs = string.Empty;
        using (SqlConnection connection = new SqlConnection())
        {
            connection.ConnectionString = GlobalVar.ConnString;
            connection.Open();
            // Use the connection
            using (SqlCommand command = new SqlCommand())
            {
                command.CommandText = query;
                command.Connection = connection;
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    StringBuilder sb = new StringBuilder();
                    while (reader.Read())
                    {
                        rs += ""+ reader.GetGuid(0) +"";
                    }
                }
                //SqlDataAdapter adapter = new SqlDataAdapter(command.CommandText, connection.ConnectionString);
                //DataSet customers = new DataSet();
                //adapter.Fill(customers, "user");
                //rs += "<count name='row'>" + customers.Tables["user"].Rows.Count + "</count>";
                //rs += customers.GetXml();
            }
            connection.Close();
        }
        return rs;
    }
    public void Update()
    {
        using (SqlConnection connection = new SqlConnection())
        {
            connection.ConnectionString = GlobalVar.ConnString;
            connection.Open();
            // Use the connection
            using (SqlCommand command = new SqlCommand())
            {
                command.CommandText = query;
                command.Connection = connection;
                command.ExecuteNonQuery();
            }
            connection.Close();
        }
    }
}
